/* main.c */

#include "board_init.h"

/*
 * Macros.
 */

/*
 * Variables.
 */

ble_cmd_val_t app_ble_cmd_pre = ble_cmd_val_stop;
ble_cmd_val_t app_ble_cmd_cur = ble_cmd_val_stop;

/*
 * Declerations.
 */



/*
 * Functions.
 */
int main(void)
{
    uint32_t speed = 800u; /* 80% of max. */
    
    BOARD_Init();
    printf("minicar.\r\n");
    
    /* buzzer. */
    buzzer_init();
    
    /* ws2812b. */
    ws2812b_init();
    
    /* motor. */
    motor_init();
    motor_set_speed(speed);
    
    /* ble. */
    ble_init();
    
    /* monitor */
    monitor_init();
    
#if 0
    while (1)
    {
        /* red. */
        getchar();
        memset(app_ws2812b_buff, WS2812B_BIT0_CODE, APP_WS2812B_BUFF_SIZE);
        ws2812b_prepare_led_color(app_ws2812b_buff, 0, 0xff);
        ws2812b_reflesh(app_ws2812b_buff, APP_WS2812B_BUFF_SIZE);
        printf("blue.\r\n");
        
        /* green. */
        getchar();
        memset(app_ws2812b_buff, WS2812B_BIT0_CODE, APP_WS2812B_BUFF_SIZE);
        ws2812b_prepare_led_color(app_ws2812b_buff, 0, 0xff00);
        ws2812b_reflesh(app_ws2812b_buff, APP_WS2812B_BUFF_SIZE);
        printf("red.\r\n");
        
        /* blue. */
        getchar();
        memset(app_ws2812b_buff, WS2812B_BIT0_CODE, APP_WS2812B_BUFF_SIZE);
        ws2812b_prepare_led_color(app_ws2812b_buff, 0, 0xff0000);
        ws2812b_reflesh(app_ws2812b_buff, APP_WS2812B_BUFF_SIZE);
        printf("green.\r\n");
    }
#endif
#if 0
    while (0)
    {
        getchar();
        motor_prepare_move(0u, motor_direction_stop);
        motor_update_all();
        printf("motor_direction_stop.\r\n");
        
        getchar();
        motor_prepare_move(0u, motor_direction_forward);
        motor_update_all();
        printf("motor_direction_forward.\r\n");
        
        getchar();
        motor_prepare_move(0u, motor_direction_stop);
        motor_update_all();
        printf("motor_direction_stop.\r\n");
        
        getchar();
        motor_prepare_move(0u, motor_direction_backward);
        motor_update_all();
        printf("motor_direction_backward.\r\n");
        
    }
#endif
    
/*
    0 -- 1
    2 -- 3
*/
    
    
    
    while (1)
    {
        app_ble_cmd_cur = ble_get_cmd();
        if (app_ble_cmd_cur == app_ble_cmd_pre)
        {
            continue;
        }
        
        switch (app_ble_cmd_cur)
        {
            case ble_cmd_val_stop:
                motor_prepare_move(0u, motor_direction_stop);
                motor_prepare_move(1u, motor_direction_stop);
                motor_prepare_move(2u, motor_direction_stop);
                motor_prepare_move(3u, motor_direction_stop);
                motor_update_all();
                break;

            case ble_cmd_val_forward:
                motor_prepare_move(0u, motor_direction_forward);
                motor_prepare_move(1u, motor_direction_forward);
                motor_prepare_move(2u, motor_direction_forward);
                motor_prepare_move(3u, motor_direction_forward);
                motor_update_all();
                break;
            
            case ble_cmd_val_backward:
                motor_prepare_move(0u, motor_direction_backward);
                motor_prepare_move(1u, motor_direction_backward);
                motor_prepare_move(2u, motor_direction_backward);
                motor_prepare_move(3u, motor_direction_backward);
                motor_update_all();
                break;

            case ble_cmd_val_left:
                motor_prepare_move(0u, motor_direction_backward);
                motor_prepare_move(1u, motor_direction_forward);
                motor_prepare_move(2u, motor_direction_forward);
                motor_prepare_move(3u, motor_direction_backward);
                motor_update_all();
                break;

            case ble_cmd_val_right:
                motor_prepare_move(0u, motor_direction_forward);
                motor_prepare_move(1u, motor_direction_backward);
                motor_prepare_move(2u, motor_direction_backward);
                motor_prepare_move(3u, motor_direction_forward);
                motor_update_all();
                break;

            case ble_cmd_val_left_turning:
                motor_prepare_move(0u, motor_direction_backward);
                motor_prepare_move(1u, motor_direction_forward);
                motor_prepare_move(2u, motor_direction_backward);
                motor_prepare_move(3u, motor_direction_forward);
                motor_update_all();
                break;

            case ble_cmd_val_right_turning:
                motor_prepare_move(0u, motor_direction_forward);
                motor_prepare_move(1u, motor_direction_backward);
                motor_prepare_move(2u, motor_direction_forward);
                motor_prepare_move(3u, motor_direction_backward);
                motor_update_all();
                break;

            case ble_cmd_val_speed_add:
                speed = motor_get_speed() + 100u;
                motor_set_speed(speed);
                break;

            case ble_cmd_val_speed_minus:
                speed = motor_get_speed();
                if (speed >= 100u)
                {
                    speed -= 100u;
                }
                motor_set_speed(speed);
                break;

            case ble_cmd_val_buzzer_on:
                buzzer_on();
                break;
                
            case ble_cmd_val_buzzer_off:
                buzzer_off();
                break;
            
            default: /* ble_cmd_val_stop. */
                break;
        }
        
        app_ble_cmd_pre = app_ble_cmd_cur;

    }
}

/* EOF. */

